package org.biubiu0002.baiblog.core.api;

import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.biubiu0002.baiblog.core.model.entity.MessageEntity;
import org.biubiu0002.baiblog.core.service.MessageService;
import org.biubiu0002.baiblog.im.api.MessageApiService;
import org.biubiu0002.baiblog.im.dto.message.Message;
import org.biubiu0002.baiblog.im.dto.message.MessageDto;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: biubiu0002
 * @Date: 2022/10/25 19:51
 **/
@DubboService
public class MessageApiServiceImpl implements MessageApiService {

    @Resource
    private MessageService messageService;

    @Override
    public List<MessageDto> getHistoryMsg(Long lastMsgId, String uid, String toChatUid,Integer type) {
        Preconditions.checkArgument(StringUtils.isNotBlank(uid),"用户id不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(toChatUid),"与其聊天用户id不能为空");
        Preconditions.checkArgument(!StringUtils.equals(uid,toChatUid), "两个用户id不能一致");
        List<MessageEntity> historyMsg = messageService.getHistoryMsg(lastMsgId,uid,toChatUid,type);
        return historyMsg.stream().map(item -> {
            MessageDto messageDto = new MessageDto();
            BeanUtils.copyProperties(item, messageDto);
            return messageDto;
        }).collect(Collectors.toList());
    }

    @Override
    public void savePrivateChatMsg(Message chatMsg) {
        messageService.saveHistoryChatMsg(chatMsg);
    }

    @Override
    public void saveGroupChatMsg(Message message) {
        messageService.saveHistoryChatMsg(message);

    }
}
